<?php
require_once dirname(__FILE__) . "/../includes/config.php";
require_once PHP_ROOTPATH.'/classes/database.php';
//récupération des localisation et des type_mutation pour les mutations

header("Content-Type: text/xml; charset=utf-8");
echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
echo "<list>";

$genes = (isset($_POST["genes"])) ? $_POST["genes"] : NULL;
$select = (isset($_POST["type"])) ? html_entity_decode($_POST["type"]) : NULL;

if($select){
	$id_connect=Database::getInstance();
	$taille=count($genes);

	if($select=="type_mutation"){
		if($taille>0){ //des gènes ont été sélectionnés,recherche par rapport à ceux-ci
			for($i=0;$i<count($genes);$i++){
				$query_temp = "SELECT DISTINCT type_mutation FROM Mutation WHERE nom_gene='$genes[$i]' ORDER BY type_mutation";
				$res_query = pg_query($id_connect , $query_temp) or die(pg_last_error());
				while ($back = pg_fetch_assoc($res_query)) {
					echo "<item id=\"" . $back["type_mutation"] . "\" name=\"" . $back["type_mutation"] . "\" />";
				}
			}
		}
		else{
			$query_temp = "SELECT DISTINCT type_mutation FROM Mutation ORDER BY type_mutation";
			$res_query = pg_query($id_connect , $query_temp) or die(pg_last_error());
			while ($back = pg_fetch_assoc($res_query)) {
				echo "<item id=\"" . $back["type_mutation"] . "\" name=\"" . $back["type_mutation"] . "\" />";
			}
		}
	}
	else if($select=="localisation"){
		if($taille>0){ //des gènes ont été sélectionnés,recherche par rapport à ceux-ci
			for($i=0;$i<count($genes);$i++){
				$query_temp = "SELECT DISTINCT localistion FROM Mutation WHERE nom_gene='$genes[$i]' ORDER BY localisation";
				$res_query = pg_query($id_connect , $query_temp) or die(pg_last_error());
				while ($back = pg_fetch_assoc($res_query)) {
					echo "<item id=\"" . $back["localisation"] . "\" name=\"" . $back["localisation"] . "\" />";
				}
			}
		}
		else{
			$query_temp = "SELECT DISTINCT localisation FROM Mutation ORDER BY localisation";
			$res_query = pg_query($id_connect , $query_temp) or die(pg_last_error());
			while ($back = pg_fetch_assoc($res_query)) {
				echo "<item id=\"" . $back["localisation"] . "\" name=\"" . $back["localisation"] . "\" />";
			}
		}
	}
}

echo "</list>";

?>
